package com.cheer.dto;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

@Data
@ApiModel("时间区间DTO")
public class LimitedDurationDTO {

    @ApiModelProperty("开始时间")
    private Date startDate;

    @ApiModelProperty("结束时间")
    private Date endDate;

    @Override
    public String toString() {
        return "LimitedDurationDTO{" +
                "MothStartDate=" + DateUtil.format(startDate, "yyyy-MM-dd")  +
                ", MothEndDate=" + DateUtil.format(endDate, "yyyy-MM-dd") +
                '}';
    }

    /**
     * 获取当前时间半个月内是否买过限购商品
     * @return
     */
    public static LimitedDurationDTO getDurationDto(Date date){

        LimitedDurationDTO limitedDurationDTO = new LimitedDurationDTO();
        long currentTime = date.getTime();
        DateTime startMonth = DateUtil.beginOfMonth(date);
        DateTime endMonth = DateUtil.endOfMonth(date);
        long startTime = startMonth.getTime();
        long endTime = endMonth.getTime();
        long difference = endTime - startTime;
        long midMonthTime = startTime + (difference/2);
        DateTime mediumMonth = DateUtil.date(midMonthTime);
        // 当前日期是在上半个月还是在下半个月
        if( Long.compare( currentTime , midMonthTime ) == -1 ){
            limitedDurationDTO.setStartDate( startMonth );
            limitedDurationDTO.setEndDate( mediumMonth );
        }else{
            // 大于月中
            limitedDurationDTO.setStartDate( mediumMonth );
            limitedDurationDTO.setEndDate( endMonth );
        }
        // 打印返回的日期
        System.out.println(limitedDurationDTO.toString());
        return limitedDurationDTO;
    }
}
